package com.yunfei.api.upload;

import com.yunfei.api.upload.contants.UploadContants;
import com.yunfei.api.upload.result.ResultModel;
import com.yunfei.bo.AppFileBo;
import com.yunfei.mapper.crmdb.TblobtSorageMapper;
import com.yunfei.utils.DateUtil;
import com.yunfei.utils.IdUtils;
import com.yunfei.utils.contants.ApplicationConstant;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;
@Controller
public class UploadImageApiController {
    @Autowired
    private Environment env;
    @Autowired
    private TblobtSorageMapper tblobtSorageMapper;
    /**
    * @Description: 通用图片上传，附件上传接口
    * @Param: uploadType 图片上传类型：
    * @Param: lStorageid 附件ID
     * PROD -产品
     * NEWS -资讯
     * SHOP -商城
     * VIDEO -视频
     * PRODANSW -产品回复
     * CLUB -秀实俱乐部
     * CRM --CRM附件
    * @return: void
    * @Author: zhengyunfei
    * @Date: 2019/1/25
    * @time:10:32
    */
    @RequestMapping(value = "/api/upload",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ResultModel saveCardPic( HttpServletRequest request, HttpServletResponse response,String uploadType,int index){
        ResultModel resultModel=new ResultModel();
        response.setHeader("Access-Control-Allow-Origin", "*");
        String uploadPath = env.getProperty(ApplicationConstant.FILE_IMAGE_PATH);
        String imageDomain = env.getProperty(ApplicationConstant.IMAGE_SERVER_DOMAIN) + "files/";
        String lStorageid=tblobtSorageMapper.getTblobStorageSeq()+"";
        List<AppFileBo> appFileList=new ArrayList<>();
        if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            // 获得第1张图片（根据前台的name名称得到上传的文件）
            MultipartFile imgFile1 = multipartHttpServletRequest.getFile("file");
                if (!imgFile1.isEmpty()) {
                    //获取前端传来的文件名
                    String fileName = imgFile1.getOriginalFilename();
                    // 设置存放图片文件的路径
                    String today= DateUtil.getToDay();
                    try {
                        AppFileBo appFileBo=new AppFileBo();
                        appFileBo.setlStorageid(lStorageid);
                        String fileExt = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
                        appFileBo.setcFilename(fileName);//文件名
                        appFileBo.setcFileSize(imgFile1.getSize()+"");//文件大小
                        String prex=lStorageid.substring(0,lStorageid.length()-1);
                        String filePath=uploadPath+uploadType+"/"+today+"/Tblobstorage/"+prex+"/"+appFileBo.getlStorageid()+"."+fileExt;
                        appFileBo.setcFilepath(filePath);
                        try {
                            //利用文件流保存文件
                            File saveFile= new File(filePath);
                            File parentFile=saveFile.getParentFile();
                            if(!parentFile.exists()&&!parentFile.isDirectory()){
                                parentFile.mkdirs();
                            }
                            if(!saveFile.exists())
                            {
                                try {
                                    saveFile.createNewFile();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                            imgFile1.transferTo(saveFile);
                            System.out.println("文件上传路径："+filePath);
                            String appFileName=appFileBo.getlStorageid()+"."+fileExt;
                            String fileUrl = imageDomain  + "/" +today+"/Tblobstorage/"+prex+"/"+appFileName;
                            appFileBo.setFileUrl(fileUrl);
                            appFileBo.setAppFileName(appFileName);
                            System.out.println("文件访问地址=========="+fileUrl);
                            if(index==0){
                                //shiMingRenZhengFileMapper.insertFirst(appFileBo);
                            }else{
                               // shiMingRenZhengFileMapper.insertSecond(appFileBo);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {

                    }

                }


        }

        return resultModel;
    }

}
